package com.zkingsoft.services.impl;

import com.zkingsoft.services.FyService;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.model.Fy;
import com.zkingsoft.model.FyPhoto;
import com.zkingsoft.model.Users;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.dao.FyDao;
import com.zkingsoft.dao.FyPhotoDao;
import com.zkingsoft.exception.GlobleException;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-10-07 08:53
 */
@Service("fyService")
public class FyServiceImpl implements FyService {

	@Autowired
	private FyDao fyDao;

	@Autowired
	private FyPhotoDao fyPhotoDao;

	@Override
	public int add(Fy fy) {
		fy.setGlCreateTime(new Date());
		fy.setGlUserId(((Users) WebUtil.getSession().getAttribute(SystemConstance.LOGIN_KEY)).getId());
		fy.setGlCheckStatus(Dictionary.CHECK_SHTG);
		fy.setGlPublishStatus(Dictionary.PUBLISH_STATUS_SJ);
		fy.setYxzt(Dictionary.YZZT_YX);
		if (fy.getFyImage() != null && fy.getFyImage().equals("")) {
			fy.setFyImage(WebUtil.getRequest().getContextPath() + "/resource/images/sgoods_defualt.jpg");
		}
		int i = fyDao.insert(fy);
		updatePhoto(fy);
		return i;

	}

	@Override
	public int modify(Fy fy) {
		updatePhoto(fy);
		return fyDao.update(fy);

	}

	/**
	 * 
	 * @Description: 更新房源相册
	 * @author:姜友瑶
	 * @param fy
	 *            返回类型 void
	 * @date 2016年10月8日
	 */
	private void updatePhoto(Fy fy) {

		// 删除原有相册
		FyPhoto fyPhotoQuery = new FyPhoto();
		fyPhotoQuery.setFyId(fy.getId());
		fyPhotoDao.deleteByModel(fyPhotoQuery);
		// 添加新相册
		List<FyPhoto> photoLis = fy.getPhotoList();
		if (photoLis != null) {
			for (FyPhoto fyPhoto : photoLis) {
				if (fyPhoto != null && fyPhoto.getUrl() != null && fyPhoto.getUrl().length() > 1) {
					fyPhoto.setFyId(fy.getId());
					fyPhotoDao.insert(fyPhoto);
				}
			}
		}

	}

	@Override
	public int remove(List<Long> list) {

		return fyDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long id) {

		return fyDao.deleteById(id);

	}

	@Override
	public int removeByModel(Fy fy) {

		return fyDao.deleteByModel(fy);

	}

	@Override
	public List<Fy> findInPage(Fy fy, PaginationVO pageVo) {

		return fyDao.selectInPage(fy, pageVo);

	}

	@Override
	public List<Fy> findByModel(Fy fy) {

		return fyDao.selectByModel(fy);

	}

	@Override
	public int findTotal(Fy fy) {

		return fyDao.selectTotalRecord(fy);

	}

	@Override
	public Fy findById(Long id) {

		return fyDao.selectById(id);

	}

	@Override
	public int fyCheck(Long id) {
		Fy fy = fyDao.selectById(id);
		if (fy.getGlCheckStatus().equals(Dictionary.CHECK_DSH)) {
			fy.setGlCheckStatus(Dictionary.CHECK_SHTG);
			return fyDao.update(fy);
		} else {
			throw new GlobleException("该房源无法审核");
		}
	}

	@Override
	public int fyCheckBh(Long id, String msg) {
		Fy fy = fyDao.selectById(id);
		if (fy.getGlCheckStatus().equals(Dictionary.CHECK_DSH)) {
			fy.setGlCheckStatus(Dictionary.CHECK_SHWTG);
			fy.setGlCheckRemark(msg);
			return fyDao.update(fy);
		} else {
			throw new GlobleException("该房源无法审核");
		}
	}

}